package com.nsgf.library.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.nsgf.library.data.bean.Category;
import com.zhht.htzx.R;
import java.util.List;

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
    private Context context;
    private List<Category> categories;
    private OnCategoryClickListener listener;

    public interface OnCategoryClickListener {
        void onCategoryClick(Category category);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_category, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Category category = categories.get(position);
        holder.categoryName.setText(category.getName());
        
        // 设置子分类的RecyclerView
        if (category.getList() != null && !category.getList().isEmpty()) {
            holder.subCategoryRecyclerView.setVisibility(View.VISIBLE);
            SubCategoryAdapter subAdapter = new SubCategoryAdapter(context, category.getList(), subCategory -> {
                if (listener != null) {
                    listener.onCategoryClick(subCategory);
                }
            });
            holder.subCategoryRecyclerView.setAdapter(subAdapter);
        } else {
            holder.subCategoryRecyclerView.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onCategoryClick(category);
            }
        });
    }

    @Override
    public int getItemCount() {
        return categories != null ? categories.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView categoryName;
        RecyclerView subCategoryRecyclerView;

        ViewHolder(View itemView) {
            super(itemView);
            categoryName = itemView.findViewById(R.id.category_name);
            subCategoryRecyclerView = itemView.findViewById(R.id.sub_category_recycler_view);
            subCategoryRecyclerView.setLayoutManager(new GridLayoutManager(itemView.getContext(), 3));
        }
    }

    // 子分类适配器
    private static class SubCategoryAdapter extends RecyclerView.Adapter<SubCategoryAdapter.ViewHolder> {
        private Context context;
        private List<Category> subCategories;
        private OnCategoryClickListener listener;

        SubCategoryAdapter(Context context, List<Category> subCategories, OnCategoryClickListener listener) {
            this.context = context;
            this.subCategories = subCategories;
            this.listener = listener;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_sub_category, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Category subCategory = subCategories.get(position);
            holder.subCategoryName.setText(subCategory.getName());
            holder.itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onCategoryClick(subCategory);
                }
            });
        }

        @Override
        public int getItemCount() {
            return subCategories != null ? subCategories.size() : 0;
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            TextView subCategoryName;

            ViewHolder(View itemView) {
                super(itemView);
                subCategoryName = itemView.findViewById(R.id.sub_category_name);
            }
        }
    }
} 